﻿using HPIT.RentHouse.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HPIT.RentHouse.DTO;
using HPIT.RentHouse.Common;

namespace HPIT.RentHouse.Web.Controllers
{
    public class HomeController : Controller
    {
        private IServiceAdminUsers _adminUserService;
        private IHouseService _houseService;
        private IRegionService _regionService;   
        public HomeController(IServiceAdminUsers adminUserService,IHouseService houseService,
            IRegionService regionService)
        {
            _adminUserService = adminUserService;
            _houseService = houseService;
            _regionService = regionService; 
        }
        /// <summary>
        /// 启动页面加载房源
        /// </summary>
        /// <param name="cityId"></param>
        /// <returns></returns>
        public ActionResult Index(int cityId=0)
        {
            var list = _adminUserService.GetCity();
            ViewBag.CityList = list;
            if (cityId>0)
            {
                ViewBag.cityId = cityId;
                ViewBag.DefaultCity = list.Where(a => a.Id == cityId).FirstOrDefault().Name;
            }
            else
            {              
                ViewBag.DefaultCity=list.FirstOrDefault().Name; 
            }
            return View();
        }
        [HttpPost]
        public ActionResult LoadMore(int cityId,int pageIndex)
        {          
            var list = _houseService.GetHouseList(pageIndex, cityId, 5);
            foreach (var item in list)
            {
                if (!string.IsNullOrEmpty(item.FirstThumbUrl))
                {
                    item.FirstThumbUrl = CommonHelper.GetServerIP() + item.FirstThumbUrl;
                }
            }
            return Json(list);
        }
        /// <summary>
        /// 根据条件搜素房源
        /// </summary>
        /// <param name="options"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Search(HouseSearchOptions options)
        {
            //默认城市
            var cityList = _adminUserService.GetCity();
            options.CityId = cityList.FirstOrDefault().Id;  

            ViewBag.DefaultCity=cityList.FirstOrDefault().Name;
            ViewBag.RegionList = _regionService.GetRegionList(cityList.FirstOrDefault().Id);

            //月租条件处理
            if (!string.IsNullOrWhiteSpace(options.MonthRent))
            {
                var strMonthRent = options.MonthRent.Split('-');
                if (strMonthRent[0]!="*")
                {
                    options.StartMonthRent = Convert.ToInt32(strMonthRent[0]);
                }
                if (strMonthRent[1] != "*")
                {
                    options.EndMonthRent = Convert.ToInt32(strMonthRent[1]);
                }
            }

            var list = _houseService.Search(options);
            return View(list);
        }
        /// <summary>
        /// 房源详情信息展示
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Detail(long id)
        {           
            var list=_houseService.GetHouseDetail(id);  
            return View(list);
        }
    }
}